package de.tum.in.eist.poll.shared;

import java.io.Serializable;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * 
 * This class represents a single feedback postet by a user about a lecture.
 * @author andriy.stefanov@gmail.com
 *
 */
@PersistenceCapable
public class Feedback implements Serializable {
	
	@NotPersistent
	private static final long serialVersionUID = 1L;
	
	/**
	 * a dummy user object. used for anonymizing of feedbacks.
	 */
	@NotPersistent
	private final static User DUMMY = new User("","",User.STUDENT);
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;
	
	/**
	 * the answer about a lecture ('very bad', 'bad', and so on)
	 */
	@Persistent
	private int answer;
	
	/**
	 * positive answer about a lecture as text
	 */
	@Persistent
	private String positiveAnswer;
	
	/**
	 * negative answer about a lecture as text
	 */
	@Persistent
	private String negativeAnswer;
	
	/**
	 * user who has posted this feedback
	 */
	@NotPersistent
	private User author;
	
	@Persistent
	private String authorKey;
	
	@Persistent	
	private String lectureKey;

	/**
	 * create new empty Feedback object (needed by serialization)
	 */
	public Feedback(){
		positiveAnswer = "";
		negativeAnswer = "";
	}
	
	/**
	 * create new Feedback object without text answers.
	 * @param autor - user who has posted this feedback
	 * @param answer - answer to the poll
	 */
	public Feedback(User author, int answer){
		this.answer = answer;
		this.author = author;
		this.authorKey = author.getKey();
	}
	
	/**
	 * create new Feedback object with poll and text answers.
	 * @param autor - user who has posted this feedback
	 * @param answer - answer to the poll
	 * @param positiveAnswer - positive comment about the lecture
	 * @param negativeAnswer - negative comment about the lecture
	 */
	public Feedback(User author, int answer, String positiveAnswer,
			String negativeAnswer) {
		this(author,answer);
		this.positiveAnswer = positiveAnswer;
		this.negativeAnswer = negativeAnswer;
	}

	/**
	 * remove the author attribute from this feedback
	 */
	public void anonymize(){
		this.author = DUMMY;
	}
	
	/**
	 * remove the text comments from this feedback
	 */
	public void removeAnswers(){
		this.positiveAnswer = this.negativeAnswer = "";
	}
	
	/**
	 * Get the positive text comment.
	 * @return positive answer
	 */
	public String getPositiveAnswer() {
		return positiveAnswer;
	}
	
	/**
	 * Get the negative text comment.
	 * @return negative answer
	 */
	public String getNegativeAnswer() {
		return negativeAnswer;
	}

	/**
	 * Get the poll answer.
	 * @return poll answer
	 */
	public int getAnswer() {
		return answer;
	}

	/**
	 * change the positive Answer of this feedback object
	 * @param positiveAnswer - new positive answer as String
	 */
	public void setPositiveAnswer(String positiveAnswer) {
		this.positiveAnswer = positiveAnswer;
	}

	/**
	 * change the positive Answer of this feedback object
	 * @param positiveAnswer - new positive answer as String
	 */
	public void setNegativeAnswer(String negativeAnswer) {
		this.negativeAnswer = negativeAnswer;
	}
	
	/**
	 * Get the user who has posted this feedback.
	 * @return author user
	 */
	public User getAuthor() {
		return author;
	}
	
	/**
	 * 
	 * @return {@code true} if at least one text comment exists, {@code false} otherwise
	 */
	public boolean hasTextAnsweres(){
		return positiveAnswer.length() > 0 || negativeAnswer.length() > 0;
	}
	
	public String getKey(){
		return key;
	}
	
	public String getLectureKey() {
		return lectureKey;
	}

	public void setLectureKey(String lectureKey) {
		this.lectureKey = lectureKey;
	}
	
	public String toString(){
		return "Author: " + author + " answer: " + answer;
	}

	public String getAuthorKey() {
		return authorKey;
	}

	public void setAuthor(User author) {
		this.authorKey = author.getKey();
		this.author = author;
	}
}
